import sys
from PyQt5.QtWidgets import QApplication, QWidget,QMainWindow
from PyQt5.QtCore import pyqtSignal,QThread
from mainwindows import Ui_MainWindow
import PyQt5.QtCore
import threading
import socket

BUFSIZE = 1024

class UDPServer(QThread):
    rcv_signal = pyqtSignal(str)
    def __init__(self, ip_port):
        super(UDPServer, self).__init__()
        self.server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.server.bind(ip_port)
        self.running = True

    def run(self):
        print("start ")
        while self.running:
            data, client_addr = self.server.recvfrom(BUFSIZE)
            print("rectdata", data,client_addr)
            self.rcv_signal.emit(str(data))

class EsMainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(EsMainWindow, self).__init__()
        self.setupUi(self)
        self.send_server = UDPServer(('127.0.0.1', 9999))
        self.rcv_server = UDPServer(('127.0.0.1', 9998))
        self.send_server.rcv_signal.connect(self.rcv_requested_send_msg)
        self.send_server.start()
        self.rcv_server.rcv_signal.connect(self.rcv_send_msg)
        self.rcv_server.start()

    def rcv_requested_send_msg(self, data):
        self.sendTextBrowser.setText(data)

    def rcv_send_msg(self, data):
        self.receiveTextBrowser.setText(data)

    def start_record(self):
        self.statusLabel.setText(r"开始抓包")
        self.webAddressitelineEdit.setText(r"抓包地址")
        self.receiveTextBrowser.setText(r"开始收集信息")

    def end_record(self):
       self.statusLabel.setText(r"停止抓包")
       self.sendTextBrowser.setText("xxx")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = EsMainWindow()
    w.move(300, 300)
    w.setWindowTitle('Simple')
    w.show()
    sys.exit(app.exec_())
